package cn.edu.sjtu.se.dslab.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import cn.edu.sjtu.se.dslab.actionbean.LoginActionBean;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.pojo.User;
import cn.edu.sjtu.se.dslab.service.ILoginService;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.Message;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 系统用户管理Action处理类
 * @author 孙科
 */
public class LoginAction extends ActionSupport implements
		ModelDriven<LoginActionBean> {
	private LoginAction() {
	}

	/**
	 * 定义日志处理对象
	 */
	private static final Log log = LogFactory.getLog(LoginAction.class);

	/**
	 * 版本ID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 定义页面信息集合
	 */

	private LoginActionBean loginActionBean = new LoginActionBean();

	/**
	 * 	页面提交信息取得<BR>
	 * 	@return	页面提交信息
	 */
	public LoginActionBean getModel() {
		//返回页面信息集合
		return loginActionBean;
	}

	/**
	 * 	登陆业务类
	 */
	private ILoginService service;


	/**
	 * 	获取登陆业务类<BR>
	 *  @return	登陆业务类
	 */
	public ILoginService getService() {
		return service;
	}

	/**
	 * 	修改登陆业务类<BR>
	 *  @param	登陆业务类
	 */

	public void setService(ILoginService service) {
		this.service = service;
	}

	public LoginActionBean getLoginActionBean() {
		return loginActionBean;
	}

	public void setLoginActionBean(LoginActionBean loginActionBean) {
		this.loginActionBean = loginActionBean;
	}

	/**
	 * 	登录验证<BR>
	 * 	@return	登陆结果标志位
	 * 	@exception
	 */
	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		//获取request
				HttpServletRequest request = ServletActionContext.getRequest();
				//获取session
				HttpSession session = request.getSession();
		Map mapRequest = (Map) ActionContext.getContext().get(Const.REQUEST);
		//如果用户ID为空
		if (loginActionBean.getTxtUsername() == null
				|| loginActionBean.getTxtPassword() == null) {
			//返回登录页面
			return Const.INDEX;
		}
		try {
			// userDAO.findByLoginInfo(loginActionBean.getTxtUsername(),loginActionBean.getTxtPassword());
			// beanService.run(loginActionBean.getTxtUsername(),loginActionBean.getTxtPassword());
			// System.out.println(service.toString());
			User user = service.login(loginActionBean);
			//如果验证成功
			if (user != null) {
				// 将User放入session
				session.setAttribute(Const.SESSIONNAME, user);
				// 返回成功标志位
				return Const.LOGIN;
			}
			//如果验证不成功
			else {
				//设置出错信息
				this.addActionError(Message.LOGIN_ERROR);
				this.addFieldError("message", Message.LOGIN_ERROR);
				//log添加
				log.error(Message.LOGIN_ERROR);
				//返回错误标志位
				return Const.LOGIN_FAIL;
			}
		} catch (DBException e) {

			//设置出错信息
			this.addActionError(e.getMessage());
			this.addFieldError("message", e.getMessage());
			e.printStackTrace();
			//log添加
			log.error(e.getMessage());

			//返回错误标志位
			return Const.ERROR;
		}
	}

	/**
	 * 	退出登陆<BR>
	 * 	@return	退出登陆结果标志位
	 * 	@exception
	 */
	public String logout() throws Exception {
		//获取request
		HttpServletRequest request = ServletActionContext.getRequest();

		//获取session
		HttpSession session = request.getSession();

		//清空session
		session.removeAttribute(Const.MENU);
		session.removeAttribute(Const.SESSIONNAME);
		//添加错误信息
		this.addActionMessage(Message.LOGOUT_SUCCESS);

		//返回处理结果
		return Const.LOGINOUT;
	}

}
